package ua.kharkiv.karimov.knure.SummaryTask1.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import ua.kharkiv.karimov.knure.SummaryTask1.Airplanes.Craft;

/**
 * This class is "simulation" of the aircraft
 * 
 * @author Portable
 * 
 */
public class ClientAirControl {
	public static int updateTime = 30 * 1000; // every 30 seconds
	public static boolean state = true;
	public final Craft airplane;

	private static class ClientThreadRead extends Thread {
		public static Socket socket;
		public static BufferedReader in;
		public static PrintWriter out;
		public Craft control;

		public ClientThreadRead(Craft control) throws IOException {
			super();
			this.control = control;
			InetAddress addr = InetAddress.getByName(null);
			socket = new Socket(addr,
					ua.kharkiv.karimov.knure.SummaryTask1.server.Server.PORT);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream())), true);
			start();
		}

		public void run() {
			try {
				out.println(control.getCode() + "\t" + control.getCapacity()
						+ "\t" + control.getMaxFuel() + "\t"
						+ control.getFuelPerKm() + "\t"
						+ control.getMaxWeight() + "\t" + control.getWeight());

				while (state) {
					if (socket.isClosed())
						break;
					String str = in.readLine();
					if (str == null)
						continue;
					if (str.toLowerCase().indexOf("flyto") > -1) {
						// do smtn
					}
					if (str.toLowerCase().indexOf("see") > -1) {
						out.println(str + ":" + control);
					}
					if (str.toLowerCase().indexOf("update") > -1) {
						out.println(str.split(" update")[0] + " see:"
								+ control.getStatus());
					}
					if (str != null && str.equals("exit"))
						if (ClientThreadRead.out != null) {
							try {
								state = false;
								ClientThreadRead.socket.close();
								break;
							} catch (IOException e1) {
								e1.printStackTrace();
							}
						}

				}
				System.out.println("closing...");
			} catch (Exception e) {
				try {
					state = false;
					socket.close();
				} catch (IOException ee) {
					System.err.println("Socket not closed");
				}
			} finally {
				try {
					state = false;
					socket.close();
				} catch (IOException e) {
					System.err.println("Socket not closed");
				}
			}
		}
	}

	private class ClientThreadUpdate extends Thread {
		public Craft control;

		public ClientThreadUpdate(Craft control) {
			super();
			this.control = control;
		}

		public void run() {
			while (state) {
				try {
					if (control.getCoord().getX() > control.getAirportTo()
							.getCoord().getX())
						control.getCoord().setX(control.getCoord().getX() - 1);
					if (control.getCoord().getX() < control.getAirportTo()
							.getCoord().getX())
						control.getCoord().setX(control.getCoord().getX() + 1);
					if (control.getCoord().getY() > control.getAirportTo()
							.getCoord().getY())
						control.getCoord().setY(control.getCoord().getY() - 1);
					if (control.getCoord().getY() < control.getAirportTo()
							.getCoord().getY())
						control.getCoord().setY(control.getCoord().getY() + 1);
					if (control.getCoord().getZ() > control.getAirportTo()
							.getCoord().getZ())
						control.getCoord().setZ(control.getCoord().getZ() - 1);
					if (control.getCoord().getZ() < control.getAirportTo()
							.getCoord().getZ())
						control.getCoord().setZ(control.getCoord().getZ() + 1);
					sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public ClientAirControl(Craft airplane) {
		this.airplane = airplane;
		try {
			new ClientThreadRead(airplane);
			new ClientThreadUpdate(airplane).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
